//
//  PKPublishEntryView.swift
//  Panku
//
//  Created by mac on 2017/2/14.
//  Copyright © 2017年 Monster. All rights reserved.
//

import UIKit

class PKPublishEntryView: PKBaseView {
    
    static let sharedInstance = PKPublishEntryView()
    
    private override init(){
        super.init()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: - properties
    //callbacks
    var didClickAtPublishLiveButton:(()->Void)?
    var didClickAtPublsihShortVideoButton:(()->Void)?
    
    //UI
    lazy var publishLiveButton = PKBaseButton.createButton()
    lazy var publishShortVideoButton = PKBaseButton.createButton()
    
    lazy var glassView = UIVisualEffectView(effect:nil)
    lazy var dismissButton = PKBaseButton.createButton()
    
    override func setupView() {
        self.addSubview(glassView)
        glassView.contentView.addSubview(publishLiveButton)
        glassView.contentView.addSubview(publishShortVideoButton)
        glassView.contentView.addSubview(dismissButton)
        
        self.backgroundColor = UIColor.clear
        self.glassView.backgroundColor = UIColor.clear
        
        glassView.snp.makeConstraints { (make) in
            make.edges.equalTo(self)
        }
        
        publishLiveButton.setImage(R.image.publish_live(), for: .normal)
        publishLiveButton.backgroundColor = UIColor.black
        publishLiveButton.addBlock(for: .touchUpInside) { (_) in
            self.didClickAtPublishLiveButton!()
            PKPublishEntryView.dismiss()
        }
        
        publishLiveButton.backgroundColor = UIColor.clear
        publishLiveButton.snp.makeConstraints { (make) in
            make.left.equalTo(self.snp.left).offset(70)
            make.bottom.equalTo(self.snp.bottom).offset(-100)
        }
        
        publishShortVideoButton.backgroundColor = UIColor.clear
        publishShortVideoButton.setImage(R.image.publish_shortVideo(), for: .normal)
        publishShortVideoButton.addBlock(for: .touchUpInside) { (_) in
            self.didClickAtPublsihShortVideoButton!()
            PKPublishEntryView.dismiss()
        }
        
        publishShortVideoButton.snp.makeConstraints { (make) in
            make.right.equalTo(self.snp.right).offset(-70)
            make.bottom.equalTo(self.snp.bottom).offset(-100)
        }
        
        dismissButton.setImage(R.image.publish_dismiss(), for: .normal)
        dismissButton.addBlock(for: .touchUpInside) { (_) in
            PKPublishEntryView.dismiss()
        }
        dismissButton.snp.makeConstraints { (make) in
            make.centerX.equalTo(glassView.snp.centerX)
            make.bottom.equalTo(glassView.snp.bottom).offset(-20)
        }
    }
    
    
    //MARK: - showIn and dismiss
    class func showIn(view:UIView = KEY_WINDOW!) -> Void{
        
        let entryView = PKPublishEntryView.sharedInstance
        view.addSubview(entryView)
        
        entryView.publishLiveButton.transform = CGAffineTransform(scaleX: 0, y: 0)
        entryView.publishShortVideoButton.transform = CGAffineTransform(scaleX: 0, y: 0)
        entryView.dismissButton.transform = CGAffineTransform(scaleX: 0, y: 0)
        entryView.frame = APP_FRAME
        UIView.animate(withDuration: 0.25, animations: { 
            entryView.glassView.effect = UIBlurEffect(style: .dark)

        }) { (_) in
            UIView.animate(withDuration: 0.25) {

                entryView.publishLiveButton.transform = CGAffineTransform(scaleX: 1, y: 1)
                entryView.publishShortVideoButton.transform = CGAffineTransform(scaleX: 1, y: 1)
                entryView.dismissButton.transform = CGAffineTransform(scaleX: 1, y: 1)

            }
        }
    }
    
    class func dismiss() -> Void{
        let entryView = PKPublishEntryView.sharedInstance
        entryView.publishLiveButton.transform = CGAffineTransform(scaleX: 1, y: 1)
        entryView.publishShortVideoButton.transform = CGAffineTransform(scaleX: 1, y: 1)
        entryView.dismissButton.transform = CGAffineTransform(scaleX: 1, y: 1)

        UIView.animate(withDuration: 0.25, animations: {

            entryView.publishLiveButton.transform = CGAffineTransform(scaleX: 0, y: 0)
            entryView.publishShortVideoButton.transform = CGAffineTransform(scaleX: 0, y: 0)
            entryView.dismissButton.transform = CGAffineTransform(scaleX: 0, y: 0)

            
        }) { (_) in
            UIView.animate(withDuration: 0.25, animations: { 
                entryView.glassView.effect = nil
            }, completion: { (_) in
                entryView.removeFromSuperview()
            })
        }
    }
    
    //设置点击回调
    class func setCallbacks(didClickAtPublishLiveButton:@escaping (()->Void),didClickAtPublsihShortVideoButton:@escaping (()->Void)) -> Void{
        let entryView = PKPublishEntryView.sharedInstance
        entryView.didClickAtPublishLiveButton = didClickAtPublishLiveButton
        entryView.didClickAtPublsihShortVideoButton = didClickAtPublsihShortVideoButton
    }

    
}
